import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:loadmore/loadmore.dart';
import 'package:xlsk/api/model/class.dart';
import 'package:xlsk/components/empty.dart';
import 'package:xlsk/routes/xlsk_router.dart';
import 'package:xlsk/utils/dimension.dart';

class ClassList extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _classListState();
  }
}

class _classListState extends State<ClassList> {
  int count = 0;
  int page = 1;
  List classList = [];
  List nowClassList = [{}];

  @override
  void initState() {
    super.initState();
    init();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("班级",style: TextStyle(color: Color(0xFF1F2329),fontSize: Dimension(context).toPx(17),fontWeight: FontWeight.bold)),
        leading: GestureDetector(
          child: Icon(Icons.arrow_back_ios,color: Color.fromRGBO(31, 35, 41, 1),size: 24,),
          onTap: (){
            XlskRouter.back(context);
          },
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
      ),
      body: RefreshIndicator(
        onRefresh: () async{
          init();
        },
        child: Container(
          width: Dimension(context).toVw(100),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                child: classList.length > 0?Container(
                  width: Dimension(context).toPx(343),
                  color: Color(0xfff7f8fa),
                  child: LoadMore(
                    isFinish: nowClassList.length==0?true:false,
                    onLoadMore: getClassList,
                    child: ListView.builder(
                      itemCount: count,
                      itemBuilder: (BuildContext context,int index){
                        return GestureDetector(
                          onTap: (){
                            XlskRouter.go(context, '/class/info?id=${classList[index]['id']}');
                          },
                          child: Container(
                            padding: EdgeInsets.all(16),
                            margin: EdgeInsets.only(top: Dimension(context).toPx(16)),
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.all(Radius.circular(Dimension(context).toPx(12)))
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Expanded(
                                  child: Container(
                                    height: Dimension(context).toPx(46),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        Text('${classList[index]['name']}',style: TextStyle(color: Color(0xff1f2329),fontSize: Dimension(context).toPx(16),fontWeight: FontWeight.w700),maxLines: 1,overflow: TextOverflow.ellipsis),
                                        Text('学员数：${classList[index]['stu_count']}/${classList[index]['rongliang']}',style: TextStyle(color: Color(0xff8f959e),fontSize: Dimension(context).toPx(12)))
                                      ],
                                    ),
                                  ),
                                ),
                                Container(
                                  width: Dimension(context).toPx(58),
                                  height: Dimension(context).toPx(26),
                                  decoration: BoxDecoration(
                                      color: Color(0xff1661ff),
                                      borderRadius: BorderRadius.all(Radius.circular(Dimension(context).toPx(4)))
                                  ),
                                  child: Center(
                                    child: Text('查看',style: TextStyle(color: Colors.white,fontSize: Dimension(context).toPx(13))),
                                  ),
                                )
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  ),
                ):Empty('暂无班级'),
              )
            ],
          ),
        ),
      )
    );
  }

  void init(){
    setState(() {
      page = 1;
      classList = [];
      getClassList();
    });
  }

  Future<bool> getClassList() async{
    int _page = page;
    List<dynamic> _classList = classList;
    Map<String,dynamic> data = {
      "limit":10,
      "page":page
    };
    var res = await classApi().getClassList(context, data);
    if(res.length > 0){
      _page++;
      _classList.addAll(res);
    }
    setState(() {
      classList = _classList;
      nowClassList = res;
      page = _page;
    });
    setState(() {
      count = _classList.length;
    });
    return true;
  }

  @override
  void dispose() {
    super.dispose();
  }
}
